import pandas as pd

df = pd.read_csv('property-data.csv')
print(df['NUM_BEDROOMS'])
print(df['NUM_BEDROOMS'].isnull())

missing_values = ["n/a", "na", "--"]
df = pd.read_csv('property-data.csv', na_values=missing_values)
print(df['NUM_BEDROOMS'])
print(df['NUM_BEDROOMS'].isnull())

df = pd.read_csv('property-data.csv')
new_df = df.dropna()
print(new_df.to_string())

# 如果你要修改源数据 DataFrame, 可以使用 inplace = True 参数:
df = pd.read_csv('property-data.csv')
df.dropna(inplace=True)
print(df.to_string())

df = pd.read_csv('property-data.csv')
df.dropna(subset=['ST_NUM'], inplace=True)
print(df.to_string())

df = pd.read_csv('property-data.csv')
df.fillna(12345, inplace=True)
print(df.to_string())

df = pd.read_csv('property-data.csv')
df['PID'].fillna(12345, inplace=True)
print(df.to_string())

df = pd.read_csv('property-data.csv')
# 平均数
x = df["ST_NUM"].mean()
# 中位数
x = df["ST_NUM"].median()
# 众数
x = df["ST_NUM"].mode()
df["ST_NUM"].fillna(x, inplace=True)
print(df.to_string())

# 第三个日期格式错误
data = {
    "Date": ['2020/12/01', '2020/12/02', '20201226'],
    "duration": [50, 40, 45]
}

df = pd.DataFrame(data, index=["day1", "day2", "day3"])
df['Date'] = pd.to_datetime(df['Date'])
print(df.to_string())

person = {
    "name": ['Google', 'Runoob', 'Taobao'],
    "age": [50, 40, 12345]  # 12345 年龄数据是错误的
}
df = pd.DataFrame(person)
df.loc[2, 'age'] = 30  # 修改数据
print(df.to_string())

person = {
    "name": ['Google', 'Runoob', 'Taobao'],
    "age": [50, 200, 12345]
}
df = pd.DataFrame(person)
for x in df.index:
    if df.loc[x, "age"] > 120:
        df.loc[x, "age"] = 120
print(df.to_string())

person = {
    "name": ['Google', 'Runoob', 'Taobao'],
    "age": [50, 40, 12345]  # 12345 年龄数据是错误的
}
df = pd.DataFrame(person)
for x in df.index:
    if df.loc[x, "age"] > 120:
        df.drop(x, inplace=True)
print(df.to_string())

# 如果对应的数据是重复的，duplicated() 会返回 True，否则返回 False。
person = {
    "name": ['Google', 'Runoob', 'Runoob', 'Taobao'],
    "age": [50, 40, 40, 23]
}
df = pd.DataFrame(person)
print(df.duplicated())

# 删除重复数据，可以直接使用drop_duplicates() 方法。
persons = {
    "name": ['Google', 'Runoob', 'Runoob', 'Taobao'],
    "age": [50, 40, 40, 23]
}
df = pd.DataFrame(persons)
df.drop_duplicates(inplace=True)
print(df)
